import { CopyControllerBase } from "./CopyControllerBase";
import { Language } from "../../../../../extend/language/Language";
import { SRobCityTwoJoinTeam } from "../../../../../Message/Public/SRobCityTwoJoinTeam";
import { KingChampionShipRankPanel } from "../kingChampionShip/KingChampionShipRankPanel";
import { KingChampionShipInfoPanel } from "../kingChampionShip/KingChampionShipInfoPanel";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../../../Message/Command/EPublicCommand";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { TCopy } from "../../../../../Message/Db/Tables/TCopy";
import { GameController } from "../../../mvc/GameController";
import { DataEvent } from "../../../events/DataEvent";
import { MessageBlock } from "../../../../../Framework/MQ/MessageBlock";
import { SRobCityTwoStart } from "../../../../../Message/Public/SRobCityTwoStart";
import { ClockManager } from "../../../manager/ClockManager";
import { ThingUtil } from "../../../scene/layer/utils/ThingUtil";
import { MsgManager } from "../../../manager/MsgManager";
import { SRobCityEndReward } from "../../../../../Message/Public/SRobCityEndReward";
import { SRobCityTwoJoinTeamInfo } from "../../../../../Message/Public/SRobCityTwoJoinTeamInfo";
import { SRobCityTwoRanks } from "../../../../../Message/Public/SRobCityTwoRanks";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { GRCTwoRankData } from "../guildRobCityCopy/copyTwo/GRCTwoRankData";
import { ECopyType } from "../../../../../Message/Public/ECopyType";
import { KingChampionShipHitBtn } from "../kingChampionShip/KingChampionShipHitBtn";
import { GameProxy } from "../../../mvc/GameProxy";
import { KingChampionShipEnterWin } from "../kingChampionShip/KingChampionShipEnterWin";
import { KingChampionShipResultWin } from "../kingChampionShip/KingChampionShipResultWin";
type int = number;
//class KingChampionShipController
    
    export  class KingChampionShipController extends CopyControllerBase
    {
        constructor()
        {
            super();
            return;
        }

        /* internal  */parseGuildRankResult(): Array<any>
        {
            var loc3=null;
            var loc4=null;
            var loc5=null;
            var loc1=[];
            cache.kingChampionShip.forceInfo.robCityTwoJoinTeams.sort(this.sortGuildResult);
            var loc2=0;
            while (loc2 < cache.kingChampionShip.forceInfo.robCityTwoJoinTeams.length) 
            {
                loc3 = cache.kingChampionShip.forceInfo.robCityTwoJoinTeams[loc2];
                if (loc3 != null) 
                {
                    (loc4 = {})["rank"] = loc3.rank;
                    loc4["guildName"] = loc3.guildName;
                    loc4["score"] = loc3.score;
                    loc1.push(loc4);
                }
                ++loc2;
            }
            loc1.sort(this.shorguildRnak);
            loc2 = 0;
            while (loc2 < loc1.length) 
            {
                (loc5 = loc1[loc2])["rank"] = loc2 + 1;
                if (loc2 != 0) 
                {
                    if (loc2 != 1) 
                    {
                        if (loc2 != 2) 
                        {
                            loc5["job"] = Language.getString(80413);
                        }
                        else 
                        {
                            loc5["job"] = Language.getString(80412);
                        }
                    }
                    else 
                    {
                        loc5["job"] = Language.getString(80411);
                    }
                }
                else 
                {
                    loc5["job"] = Language.getString(80410);
                }
                ++loc2;
            }
            return loc1;
        }

        /* internal  */shorguildRnak(arg1: Object, arg2: Object): int
        {
            if (int(arg1["rank"]) < int(arg2["rank"])) 
            {
                return -1;
            }
            if (int(arg1["score"]) > int(arg2["score"])) 
            {
                return -1;
            }
            return 1;
        }

        /* internal  */sortGuildResult(arg1: SRobCityTwoJoinTeam, arg2: SRobCityTwoJoinTeam): int
        {
            if (arg1.rank <= arg2.rank) 
            {
                return -1;
            }
            return 1;
        }

        /* internal  */get killRankPanel(): KingChampionShipRankPanel
        {
            if (this._killRankPanel == null) 
            {
                this._killRankPanel = new KingChampionShipRankPanel();
            }
            return this._killRankPanel;
        }

        /* internal  */get infoPanel(): KingChampionShipInfoPanel
        {
            if (!this._infoPanel) 
            {
                this._infoPanel = new KingChampionShipInfoPanel();
            }
            return this._infoPanel;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(EPublicCommand._ECmdKingChampionshipPrepare, this.prepearHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdKingChampionshipStart, this.startHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdKingChampionshipEnd, this.endHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobCityEndReward, this.showFinalRankHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicKingChampionshipForce, this.forceInfoHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicKingChampionshipRank, this.onReceiveCopyTwoRank);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicKingChampionshipScore, this.copyInfoUpdateHandler);
            Dispatcher.addEventListener(EventName.KingChampionShipEnterCopyReq, this.askEnterCopyHandler);
            Dispatcher.addEventListener(EventName.KingChampionShipShowEntranceReq, this.askShowEntranceHandler);
            Dispatcher.addEventListener(EventName.KingChampionShipShowRankReq, this.onShowCopyTwoRankPanel);
            Dispatcher.addEventListener(EventName.KingChampionShipAskPersionalRank, this.onAskPersionalRankReq);
            return;
        }

        public /* override */ enterCopy(arg1: TCopy): void
        {
            super.enterCopy(arg1);
            this.infoPanel.updatePosition();
            this.infoPanel.show();
            GameController.gameui.getGameui().showAllUI(false);
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, false));
            if (!(this._infoPanel == null) && !(cache.kingChampionShip.copyInfo == null)) 
            {
                this._infoPanel.updateLeftTime(cache.kingChampionShip.copyInfo, cache.kingChampionShip.isStarted);
            }
            if (this._enterWin && !this._enterWin.isHide) 
            {
                this._enterWin.hide();
            }
            if (this._icon != null) 
            {
                this._icon.showkingChampionShipEffect(false);
            }
            return;
        }

        public /* override */ leaveCopy(): void
        {
            super.leaveCopy();
            if (this._infoPanel) 
            {
                this._infoPanel.hide();
                this._infoPanel.clean();
                this._infoPanel = null;
            }
            if (this._killRankPanel) 
            {
                this._killRankPanel.hide();
                this._killRankPanel.dispose();
                this._killRankPanel = null;
            }
            GameController.gameui.getGameui().showAllUI(true);
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, true));
            return;
        }

        protected /* override */ addListener(): void
        {
            super.addListener();
            return;
        }

        protected /* override */ removeListener(): void
        {
            super.removeListener();
            return;
        }

        /* internal  */onAcitveTimeUpdateHandler(arg1: DataEvent): void
        {
            if (this.icon.isHide) 
            {
                this.icon.show();
            }
            return;
        }

        /* internal  */prepearHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SRobCityTwoStart;
            cache.kingChampionShip.isStarted = false;
            cache.kingChampionShip.copyInfo = loc1;
            this.icon.showkingChampionShipEffect(true);
            var loc2=int((loc1.startDt.time - ClockManager.instance.nowDate.time) / 1000);
            this.icon.setLeftTime(loc2);
            this.icon.show();
            if (this._enterWin != null) 
            {
                this._enterWin.setParseAndLeftTime(Language.getString(44255), loc2);
            }
            if (this._infoPanel != null) 
            {
                this._infoPanel.updateLeftTime(loc1, false);
            }
            return;
        }

        /* internal  */startHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SRobCityTwoStart;
            cache.kingChampionShip.isStarted = true;
            cache.kingChampionShip.copyInfo = loc1;
            this.icon.showkingChampionShipEffect(true);
            this.icon.show();
            var loc2=int((loc1.startDt.time - ClockManager.instance.nowDate.time) / 1000);
            if (this._enterWin != null) 
            {
                this._enterWin.setParseAndLeftTime(Language.getStringByParam(44367, ""), loc2);
            }
            if (this._infoPanel != null) 
            {
                this._infoPanel.updateLeftTime(loc1, true);
            }
            if (cache.copy.isInKingChampionShip()) 
            {
                ThingUtil.isEntitySort = true;
                if (this._infoPanel) 
                {
                    this._infoPanel.updateLeftTime(cache.kingChampionShip.copyInfo, true);
                    MsgManager.showTaskTarget(Language.getStringByParam(60972, cache.copy.copyInfo.name));
                }
            }
            return;
        }

        /* internal  */endHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SRobCityEndReward;
            cache.kingChampionShip.isStarted = false;
            cache.kingChampionShip.copyInfo = null;
            if (this._icon != null) 
            {
                this._icon.hide();
                this._icon = null;
            }
            return;
        }

        /* internal  */showFinalRankHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SRobCityEndReward;
            cache.kingChampionShip.isStarted = false;
            cache.kingChampionShip.copyInfo = null;
            if (!cache.copy.isInKingChampionShip()) 
            {
                return;
            }
            this.showFinalRankResult(loc1);
            if (this._infoPanel) 
            {
                this._infoPanel.stopCountDown();
            }
            if (this._icon != null) 
            {
                this._icon.hide();
                this._icon = null;
            }
            return;
        }

        /* internal  */forceInfoHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SRobCityTwoJoinTeamInfo;
            cache.kingChampionShip.forceInfo = loc1;
            return;
        }

        /* internal  */onReceiveCopyTwoRank(arg1: MessageBlock): void
        {
            if (!cache.copy.isInKingChampionShip()) 
            {
                return;
            }
            var loc1=arg1.messageBase as SRobCityTwoRanks;
            if (this._killRankPanel && !this._killRankPanel.isHide) 
            {
                if (loc1.type == 1) 
                {
                    this._killRankPanel.updateRankData(0, this.getKillRankData(loc1), loc1.targetPage, loc1.totalPage);
                }
            }
            if (this._resultWin) 
            {
                this._resultWin.setData2(loc1);
            }
            return;
        }

        /* internal  */getKillRankData(arg1: SRobCityTwoRanks): DataProvider
        {
            var loc1=0;
            var loc2=null;
            if (this._killRankDp) 
            {
                this._killRankDp.removeAll();
            }
            else 
            {
                this._killRankDp = new DataProvider();
            }
            loc1 = 0;
            while (loc1 < arg1.robCityTwoPlayers.length) 
            {
                loc2 = new GRCTwoRankData(arg1.robCityTwoPlayers[loc1]);
                loc2.rank = (arg1.targetPage - 1) * 10 + (loc1 + 1);
                loc2.guildName = cache.kingChampionShip.getGuildName(loc2.force);
                this._killRankDp.addItem(loc2);
                ++loc1;
            }
            return this._killRankDp;
        }

        /* internal  */copyInfoUpdateHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SRobCityTwoJoinTeamInfo;
            cache.kingChampionShip.forceInfo = loc1;
            if (cache.copy.isInCopyType(ECopyType._ECopyKingChampionship)) 
            {
                if (this._infoPanel) 
                {
                    this._infoPanel.updateInfos(loc1);
                }
            }
            return;
        }

        /* internal  */get icon(): KingChampionShipHitBtn
        {
            if (this._icon == null) 
            {
                this._icon = new KingChampionShipHitBtn();
            }
            return this._icon;
        }

        /* internal  */iconStatusUpdateHandler(arg1: Object): void
        {
            return;
        }

        /* internal  */askEnterCopyHandler(arg1: DataEvent): void
        {
            if (cache.guild.guildInfo == null) 
            {
                MsgManager.showRollTipsMsg(Language.getString(41802));
                return;
            }
            GameProxy.copy.enterKingChampionShip();
            return;
        }

        /* internal  */askShowEntranceHandler(arg1: DataEvent): void
        {
            if (this._enterWin == null) 
            {
                this._enterWin = new KingChampionShipEnterWin();
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildGetInfo, 0));
            this._enterWin.show();
            var loc1=cache.kingChampionShip.copyInfo;
            if (loc1 == null) 
            {
                return;
            }
            var loc2=int((loc1.startDt.time - ClockManager.instance.nowDate.time) / 1000);
            if (!(this._enterWin == null) && cache.kingChampionShip.isStarted) 
            {
                this._enterWin.setParseAndLeftTime(Language.getStringByParam(44367, ""), loc2);
            }
            else if (!(this._enterWin == null) && !cache.kingChampionShip.isStarted) 
            {
                this._enterWin.setParseAndLeftTime(Language.getStringByParam(44255, ""), loc2);
            }
            return;
        }

        /* internal  */onShowCopyTwoRankPanel(arg1: DataEvent): void
        {
            if (!cache.copy.isInKingChampionShip()) 
            {
                return;
            }
            this.killRankPanel.show();
            this.killRankPanel.showRank(0);
            return;
        }

        /* internal  */onAskPersionalRankReq(arg1: DataEvent): void
        {
            return;
        }

        /* internal  */showFinalRankResult(arg1: SRobCityEndReward): void
        {
            if (cache.kingChampionShip.forceInfo == null) 
            {
                return;
            }
            if (this._resultWin == null) 
            {
                this._resultWin = new KingChampionShipResultWin();
            }
            this._resultWin.setData1(this.parseGuildRankResult());
            this._resultWin.show();
            return;
        }

        /* internal */ /* var */_enterWin: KingChampionShipEnterWin;

        /* internal */ /* var */_icon: KingChampionShipHitBtn;

        /* internal */ /* var */_resultWin: KingChampionShipResultWin;

        /* internal */ /* var */_killRankPanel: KingChampionShipRankPanel;

        /* internal */ /* var */_infoPanel: KingChampionShipInfoPanel;

        /* internal */ /* var */_killRankDp: DataProvider;
    }
